package com.freework.report.base.service.reportLoad;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Element;

import com.freework.report.util.XMLUtil;

public class BeanCreate {

	String clazz=null;
	String beanid=null;
	String typecode=null;
	SpringBean springBean=null;
	private static final Log logger = LogFactory.getLog(BeanCreate.class);
	public <T>  T createBean(Class<T> type){
		Object obj=null;
		if(beanid!=null){
			obj=springBean.getBean(beanid);
			if(type.isAssignableFrom(obj.getClass()))
				return (T)obj;
		}
		if(clazz!=null){
			try {
				obj=Class.forName(clazz).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
				logger.error(clazz, e);
			} catch (IllegalAccessException e) {
				logger.error(clazz, e);
			} catch (ClassNotFoundException e) {
				logger.error(clazz, e);
			}
			if(type.isAssignableFrom(obj.getClass()))
				return (T)obj;
		}
		
		return null;
	}
	public BeanCreate(Element element,String typecodename,SpringBean springBean){
		 typecode=XMLUtil.getAttributeValue(element, typecodename);
		 clazz=XMLUtil.getAttributeValue(element, "class");
		 beanid=XMLUtil.getAttributeValue(element, "beanid");
		 this.springBean=springBean;
		
	}
	
	public BeanCreate(String clazz, String beanid, String typecode,SpringBean springBean) {
		super();
		this.clazz = clazz;
		this.beanid = beanid;
		this.typecode = typecode;
		this.springBean=springBean;
	}
	
	public String getClazz() {
		return clazz;
	}
	public void setClazz(String clazz) {
		this.clazz = clazz;
	}
	public String getBeanid() {
		return beanid;
	}
	public void setBeanid(String beanid) {
		this.beanid = beanid;
	}
	public String getTypecode() {
		return typecode;
	}
	public void setTypecode(String typecode) {
		this.typecode = typecode;
	}
	
	
}
